package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.friend.FriendHandle;
import yxy.game.pm2.module.friend.FriendModule;

import java.util.Set;

/** 好友 好友列表 */
@MessageType(proto = Command.FriendList, classtype = FriendMessage.FriendListRequest.class)
public class CMD_FriendList extends Command<FriendMessage.FriendListRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendListRequest request) {
		String playerCode = messageSource.getTag();

		FriendMessage.FriendListResponse.Builder response = FriendMessage.FriendListResponse.newBuilder();

		FriendModule friendModule = FriendModule.get(playerCode);
		Set<String> all = friendModule.getFriend();
		FriendHandle friendHandle = new FriendHandle();
		for (String friendCode : all) {
			FriendMessage.Friend friend = friendHandle.packFriend(friendModule, friendCode);
			response.addFriends(friend);
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, FriendMessage.FriendListResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}